﻿using full_leaf_framework.Visual;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace bmfont;

public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;

    public Drawable reimu;
    public Camera camera;

    private const int SCREEN_WIDTH = 800;
    private const int SCREEN_HEIGHT = 600;

    public AnimationTrackController tracks;
    public AnimationTrack reimuTrack;
    public float animationTime;

    public BmfontController fonts;

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this)
        {
            PreferredBackBufferWidth = SCREEN_WIDTH,
            PreferredBackBufferHeight = SCREEN_HEIGHT
        };
        _graphics.ApplyChanges();
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);
        var playerTexture = Content.Load<Texture2D>("reimu");
        var sprite = new AnimatedSprite(playerTexture);
        // 创建精灵对象
        reimu = new Drawable(sprite, new Vector2(0, 0), new Vector2(0, 0), 1f, 0, SpriteEffects.None, 10, null, 1);
        // 创建自定义绘制对象
        camera = new Camera(_spriteBatch, new Rectangle(0, 0, 800, 600), new Vector2(0, 0), new Vector2(800, 600));
        // 创建摄像机
        tracks = AnimationInfo.LoadAnimationInfo("Data/Animation/reimu_move.json");
        // 创建动画管理器
        reimuTrack = tracks.ReturnAnimationTrack("reimu_move");
        // 创建动画实例
        animationTime = 0f;
        // 读取字体文件
        fonts = new BmfontController("Font", "reimu_spell", Content);
    }

    protected override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
        if (Keyboard.GetState().IsKeyDown(Keys.E)) {
            animationTime = 0f;
            // 按下E按键重置动画
        }
        camera.insertObject(reimu);
        // 调用添加绘制图像委托
        reimu.currentAnimation.Update(gameTime);
        animationTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
        reimuTrack.AffectDrawable(reimu, animationTime);
        // 更新动画
        fonts.InsertDrawObjects(camera, "【梦想封印】", new Vector2(0, 100),
        BmfontDrawable.TranslateMethod.Middle, 1, true, SpriteEffects.None, 10, 1);
        // 绘制文字
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        _spriteBatch.Begin();
        camera.Draw();
        _spriteBatch.End();
        base.Draw(gameTime);
    }
}
